package com.yuezhu.adapter;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yuezhu.Model.DictionaryModel;
import com.yuezhu.Model.ProjectModel;
import com.yuezhu.R;
import com.yuezhu.activity.MyprojectActivity;
import com.yuezhu.com_volley.OkRequestQueue;
import com.yuezhu.control.TaskEngine;
import com.yuezhu.fragment.MyProjectContentFragment;
import com.yuezhu.global.ApplicationContext;
import com.yuezhu.util.LoginOut;
import com.yuezhu.util.MyToast;
import com.yuezhu.util.StringUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 健 on 2016/5/20.
 */
public class MyProjectContentAdapter<T> extends BaseAdapter {

    private boolean isShowDeleteButton = false;
    private OkRequestQueue queue;
    private Context context;
    private List<ProjectModel> list = new ArrayList<>();
    private MyProjectContentFragment fragment;

    public MyProjectContentAdapter(Context context, OkRequestQueue queue, MyProjectContentFragment fragment) {
        this.context = context;
        this.queue = queue;
        this.fragment = fragment;
    }

    public void setList(List<ProjectModel> list) {
        this.list = list;
    }

    public void setIsShowDeleteButton(boolean isShowDeleteButton) {
        this.isShowDeleteButton = isShowDeleteButton;
    }

    public boolean isShowDeleteButton() {
        return isShowDeleteButton;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_myproject_content, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ProjectModel model = list.get(position);
        holder.name.setText(model.bldgName);
        try {
            holder.content.setText("位置：" + MyprojectActivity.getCityNameById(model.cityId) + "，业态：" + DictionaryModel.getInstance().getTypeByTypeId(model.bldgType).typeName + "，定位：" + DictionaryModel.getInstance().getGradeByGradeId(model.bldgGrade).gradeName +
                    "\n地上建筑高度：" + model.bldgHeight + "m，地上楼层数:" + model.bldgFloors + "层\n地上标准层建筑面积：" + model.floorValue + "平方米");
        } catch (Exception e) {
            holder.content.setText("获取数据错误");
        }
        holder.delete.setVisibility(isShowDeleteButton ? View.VISIBLE : View.GONE);
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);  //先得到构造器
                builder.setTitle("提示"); //设置标题
                builder.setMessage("获取项目列表"); //设置内容
                builder.setCancelable(false);
                fragment.dialog = builder.create();
                fragment.dialog.show();
                Map<String, String> map = new HashMap<String, String>();
                map.put("bldgId", model.bldgId);
                TaskEngine.getInstance().startDeleteProjectTask(queue, map, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject json) {
                        try {
                            String respCode = json.getString("respCode");
                            if (!StringUtil.equal(respCode, "OK")) {
                                try {
                                    if (json.getInt("respNo") == 999) {
                                        LoginOut.LoginOut(fragment.getActivity());
                                    }
                                } catch (Exception e) {
                                    MyToast.showDefaltToast("获取数据失败");
                                }
                                if (fragment.dialog != null) {
                                    fragment.dialog.dismiss();
                                }
                            } else {
                                MyToast.showDefaltToast(json.getString("respDesc"));
                                fragment.refresh();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                });
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView name, content, delete;

        public ViewHolder(View convertView) {
            name = (TextView) convertView.findViewById(R.id.tv_project_name);
            content = (TextView) convertView.findViewById(R.id.content);
            delete = (TextView) convertView.findViewById(R.id.delete);
        }
    }

}
